import { Keyboard } from 'react-native';
import { useSetAtom } from 'jotai';
import { signedInAtom, tokenAtom, userInfoAtom } from '@/utils/storageUtil';
import { FetchUserInfo, Login } from './UserInfo';
import { useMemoizedFn, useSafeState } from '@dibin_cn/rn-hooks';
import { useNotify } from '@/hooks/useNotify';


type Store = LoginFormType

export function useAuthService(isSmsLogin = false) {
  // const navigation = useNavigation<NavigationProp<AuthStackParamList>>();

  const { failNotify, successNotify } = useNotify();


  const updateUserInfo = useSetAtom(userInfoAtom);
  const updateSignedIn = useSetAtom(signedInAtom);
  const updateToken = useSetAtom(tokenAtom);


  const [error, setError] = useSafeState('');
  const [loading, setLoading] = useSafeState(false);
  const [disabled, setDisabled] = useSafeState(false);

  const clearError = () => {
    setError('');
  };

  /** 查询用户信息 */
  const fetchUserInfo = async () => {
    try {
      const userInfo = await FetchUserInfo();
      updateUserInfo(userInfo);
      updateSignedIn(true);
    } catch (error) {
      const message = "查询用户信息失败！";
      failNotify(message);
    }
  };

  /** 用户名密码登录 */
  const login = async (values: Store) => {
    try {
      setLoading(true);
      setDisabled(true)
      const data = await Login(values);
      if (data) {
        updateToken(data);
        await fetchUserInfo();
        setLoading(false);
        setDisabled(false)
      } else {
        setLoading(false);
        setError('对不起，登录失败');
      }
    } catch (error) {
      setLoading(false);
      setDisabled(false)
      failNotify(error as string);
    }
  };

  /** 登录表单提交 */
  const handleFinish = async (values: Store) => {
    Keyboard.dismiss();
    if (isSmsLogin) {
      // smsLogin(values);
    } else {
      login(values);
    }
  };


  /** 重置密码 */
  // const forgetPassword = async (values: Store) => {
  //   try {
  //     Keyboard.dismiss();
  //     const data = await mockResetPassword(values);
  //     if (data) {
  //       navigation?.canGoBack() && navigation.goBack();
  //     } else {
  //       setError('对不起，重置密码失败');
  //     }
  //   } catch (error) {
  //     const message = convertErrorMsg(error);
  //     setError(message);
  //   }
  // };

  /** 修改密码 */
  // const modifyPassword = async (values: Store) => {
  //   try {
  //     Keyboard.dismiss();
  //     const data = await mockUpdatePassword(values);
  //     if (data) {
  //       navigation.navigate('ModifyPasswordResult');
  //     } else {
  //       failNotify('修改密码失败');
  //     }
  //   } catch (error) {
  //     const message = convertErrorMsg(error);
  //     failNotify(message);
  //   }
  // };



  /**
   * 注册
   */
  // const register = async (values: Store) => {
  //   try {
  //     Keyboard.dismiss();
  //     setLoading(true);
  //     await mockRegister(values);
  //     setLoading(false);
  //     successNotify('恭喜你，注册成功');
  //     navigation.navigate('SignIn'); // 注册成功后去登录页面登录
  //   } catch (error) {
  //     setLoading(false);
  //     const message = convertErrorMsg(error);
  //     setError(message);
  //   }
  // };

  return {
    disabled,
    loading,
    error,
    clearError: useMemoizedFn(clearError),
    setError: useMemoizedFn(setError),
    handleFinish: useMemoizedFn(handleFinish),
    // submitFormFailed: useMemoizedFn(submitFormFailed),
    // handleFormValueChange: useMemoizedFn(handleFormValueChange),
    // beforeSendSms: useMemoizedFn(beforeSendSms),
    // smsSend: useMemoizedFn(smsSend),
    // configPassword: useMemoizedFn(configPassword),
    // forgetPassword: useMemoizedFn(forgetPassword),
    // modifyPassword: useMemoizedFn(modifyPassword),
    // register: useMemoizedFn(register),
  };
}
